clo#    Copyright 2011 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
IncludePath="/opt/coopip/djangoproject/include"
if(not IncludePath in sys.path):
    sys.path.append(IncludePath)


    
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime

#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopip/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject.settings
setup_environ(djangoproject.settings)
from djangoproject.models import *
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.db import models
#********************Setup psycopg2 and django section********************
from django.db import transaction
from django import db
import datetime
import time
import sys

#script runs once a minute; roll-off prevents invalid entries from being recalculated over and over

start = datetime.datetime.now()
RollOff = start - datetime.timedelta(days=3)
Logs = VehicleLog.objects.filter(CreateDateTime__gt=RollOff).exclude(Point__isnull=True).filter(SymbologyStopStartState__isnull=True).order_by('CreateDateTime')
for i in Logs:
    if (datetime.datetime.now() - start).total_seconds() > 55:
        sys.exit(0)
    i.CalculateSymbologyStopStartState()
db.close_connection() 
sys.exit(0)








